package com.aptech.fpt.c0810g.newspj.beans;

import com.aptech.fpt.c0810g.newspj.entity.Manager;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

/**
 *
 * @author AnhGT
 */
@ManagedBean
@SessionScoped
public class ShareBean {

    private HashMap<String, Manager> sessionList = new HashMap<String, Manager>();
    private int id = 0;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public HashMap getSessionList() {
        return sessionList;
    }

    public void setSessionList(HashMap sessionList) {
        this.sessionList = sessionList;
    }

    public ShareBean() {
        this.sessionList.put("user-info", new Manager());
    }

    public void doPutElement(String key, Manager value) {
        sessionList.put(key, value);
    }

    public void doLogout() {
        try {
            this.sessionList.put("user-info", new Manager());
            FacesContext.getCurrentInstance().getExternalContext().redirect("ChangChu.url");
        } catch (IOException ex) {
            Logger.getLogger(ShareBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void checkAccessPermistion(ComponentSystemEvent event){
        if (sessionList.get("user-info").isIsLogged()!= true) {
            FacesContext fc = FacesContext.getCurrentInstance();
            ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
            handler.performNavigation("DangNhap");
        }
    }
}
